<template>
  <el-dialog
    :title="!dataForm.smsTgouId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="拼团活动主题" prop="smsTgouTitle">
      <el-input v-model="dataForm.smsTgouTitle" placeholder="拼团活动主题"></el-input>
    </el-form-item>
    <el-form-item label="拼团活动图片" prop="smsTgouImg">
      <el-input v-model="dataForm.smsTgouImg" placeholder="拼团活动图片"></el-input>
    </el-form-item>
    <el-form-item label="商品id" prop="pmsProductId">
      <el-input v-model="dataForm.pmsProductId" placeholder="商品id"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="pmsProductName">
      <el-input v-model="dataForm.pmsProductName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="商品原价" prop="pmsProductPrice">
      <el-input v-model="dataForm.pmsProductPrice" placeholder="商品原价"></el-input>
    </el-form-item>
    <el-form-item label="团购价" prop="smsTgouPrice">
      <el-input v-model="dataForm.smsTgouPrice" placeholder="团购价"></el-input>
    </el-form-item>
    <el-form-item label="限量数量" prop="smsTgouNum">
      <el-input v-model="dataForm.smsTgouNum" placeholder="限量数量"></el-input>
    </el-form-item>
    <el-form-item label="成团人数" prop="smsTgouGroupNum">
      <el-input v-model="dataForm.smsTgouGroupNum" placeholder="成团人数"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="smsTgouCreateDate">
      <el-input v-model="dataForm.smsTgouCreateDate" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="团购开始时间" prop="smsTgouStartDate">
      <el-input v-model="dataForm.smsTgouStartDate" placeholder="团购开始时间"></el-input>
    </el-form-item>
    <el-form-item label="团购结束时间" prop="smsTgouEndDate">
      <el-input v-model="dataForm.smsTgouEndDate" placeholder="团购结束时间"></el-input>
    </el-form-item>
    <el-form-item label="成团有效时间、单位分钟" prop="smsTgouDateUnit">
      <el-input v-model="dataForm.smsTgouDateUnit" placeholder="成团有效时间、单位分钟"></el-input>
    </el-form-item>
    <el-form-item label="前台是否显示 #0显示1不显示[默认0]'," prop="smsIsShow">
      <el-input v-model="dataForm.smsIsShow" placeholder="前台是否显示 #0显示1不显示[默认0]',"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          smsTgouId: 0,
          smsTgouTitle: '',
          smsTgouImg: '',
          pmsProductId: '',
          pmsProductName: '',
          pmsProductPrice: '',
          smsTgouPrice: '',
          smsTgouNum: '',
          smsTgouGroupNum: '',
          smsTgouCreateDate: '',
          smsTgouStartDate: '',
          smsTgouEndDate: '',
          smsTgouDateUnit: '',
          smsIsShow: ''
        },
        dataRule: {
          smsTgouTitle: [
            { required: true, message: '拼团活动主题不能为空', trigger: 'blur' }
          ],
          smsTgouImg: [
            { required: true, message: '拼团活动图片不能为空', trigger: 'blur' }
          ],
          pmsProductId: [
            { required: true, message: '商品id不能为空', trigger: 'blur' }
          ],
          pmsProductName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          pmsProductPrice: [
            { required: true, message: '商品原价不能为空', trigger: 'blur' }
          ],
          smsTgouPrice: [
            { required: true, message: '团购价不能为空', trigger: 'blur' }
          ],
          smsTgouNum: [
            { required: true, message: '限量数量不能为空', trigger: 'blur' }
          ],
          smsTgouGroupNum: [
            { required: true, message: '成团人数不能为空', trigger: 'blur' }
          ],
          smsTgouCreateDate: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          smsTgouStartDate: [
            { required: true, message: '团购开始时间不能为空', trigger: 'blur' }
          ],
          smsTgouEndDate: [
            { required: true, message: '团购结束时间不能为空', trigger: 'blur' }
          ],
          smsTgouDateUnit: [
            { required: true, message: '成团有效时间、单位分钟不能为空', trigger: 'blur' }
          ],
          smsIsShow: [
            { required: true, message: '前台是否显示 #0显示1不显示[默认0]',不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.smsTgouId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.smsTgouId) {
            this.$http({
              url: this.$http.adornUrl(`/tgou/tgou/info/${this.dataForm.smsTgouId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.smsTgouTitle = data.tgou.smsTgouTitle
                this.dataForm.smsTgouImg = data.tgou.smsTgouImg
                this.dataForm.pmsProductId = data.tgou.pmsProductId
                this.dataForm.pmsProductName = data.tgou.pmsProductName
                this.dataForm.pmsProductPrice = data.tgou.pmsProductPrice
                this.dataForm.smsTgouPrice = data.tgou.smsTgouPrice
                this.dataForm.smsTgouNum = data.tgou.smsTgouNum
                this.dataForm.smsTgouGroupNum = data.tgou.smsTgouGroupNum
                this.dataForm.smsTgouCreateDate = data.tgou.smsTgouCreateDate
                this.dataForm.smsTgouStartDate = data.tgou.smsTgouStartDate
                this.dataForm.smsTgouEndDate = data.tgou.smsTgouEndDate
                this.dataForm.smsTgouDateUnit = data.tgou.smsTgouDateUnit
                this.dataForm.smsIsShow = data.tgou.smsIsShow
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/tgou/tgou/${!this.dataForm.smsTgouId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'smsTgouId': this.dataForm.smsTgouId || undefined,
                'smsTgouTitle': this.dataForm.smsTgouTitle,
                'smsTgouImg': this.dataForm.smsTgouImg,
                'pmsProductId': this.dataForm.pmsProductId,
                'pmsProductName': this.dataForm.pmsProductName,
                'pmsProductPrice': this.dataForm.pmsProductPrice,
                'smsTgouPrice': this.dataForm.smsTgouPrice,
                'smsTgouNum': this.dataForm.smsTgouNum,
                'smsTgouGroupNum': this.dataForm.smsTgouGroupNum,
                'smsTgouCreateDate': this.dataForm.smsTgouCreateDate,
                'smsTgouStartDate': this.dataForm.smsTgouStartDate,
                'smsTgouEndDate': this.dataForm.smsTgouEndDate,
                'smsTgouDateUnit': this.dataForm.smsTgouDateUnit,
                'smsIsShow': this.dataForm.smsIsShow
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
